﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;

namespace tc58News
{
	public class Consumer
	{
		Channel shared;
		Dao dal = new Dao();
		public Consumer(Channel channel)
		{
			this.shared = channel;
		}

		public  void Consume()
		{
			while (true)
			{

				HtmlSource entity = shared.GetBuffer();
				if (entity == null)
				{
					Thread.Sleep(200);

				}
				else
				{
					WebRequest req = WebRequest.Create(entity.SourceUrl);
					req.Method = "get";
					WebResponse result = req.GetResponse();
					Stream sm = result.GetResponseStream();
					StreamReader sr = new StreamReader(sm, Encoding.UTF8);
					string html = sr.ReadToEnd();

					Regex reg = new Regex("<div class=\"newstitle\">([\\S\\s*]*)<div class=\"fxd\">");
					entity.Content = reg.Match(html).Groups[1].Value;
					dal.Add58tc_cn(entity);
				}
			}

		}


	}
}
